package com.sskj.b70.v.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gamerole.orcameralib.CameraActivity;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.p.activity.VerifyIdentPresenter;
import com.sskj.b70.utils.FileUtil;
import com.sskj.b70.utils.ImageUtil;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;

import java.io.File;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class VerifyIdentActivity extends BaseActivity<VerifyIdentPresenter> {


    private static final int REQUEST_A_CAMERA = 1000;
    private static final int REQUEST_B_CAMERA = 1001;
    private static final int REQUEST_C_CAMERA = 1002;
    private static final String NAME_A = "photo_a.jpg";
    private static final String NAME_B = "photo_b.jpg";
    private static final String NAME_C = "photo_c.jpg";
    @BindView(R.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R.id.verify_real_name_edittext)
    EditText mVerifyRealNameEdittext;
    @BindView(R.id.verify_ident_edittext)
    EditText mVerifyIdentEdittext;
    @BindView(R.id.verify_photo_a)
    ImageView mVerifyPhotoA;
    @BindView(R.id.verify_photo_b)
    ImageView mVerifyPhotoB;
    @BindView(R.id.verify_photo_c)
    ImageView mVerifyPhotoC;
    @BindView(R.id.verify_submit)
    Button mVerifySubmit;
    @BindView(R.id.title)
    TextView mTitle;
    private String mCurrFileName;
    private String idCardFrontImg;
    private String idCardBackImg;
    private String selfieImg;

    private HashMap<String, Object> mParams = new HashMap<String, Object>();
    private int card;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_verify_ident;
    }

    @Override
    public VerifyIdentPresenter getPresenter() {
        return new VerifyIdentPresenter();
    }

    @Override
    protected void onViewBinding() {
        mTitle.setText(R.string.verify_ident_title);
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_A_CAMERA://人证合一
                    card = 0;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_A));
                    break;
                case REQUEST_B_CAMERA://身份证正面
                    card = 1;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_B));
                    break;
                case REQUEST_C_CAMERA: //身份证背面
                    card = 2;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_C));
                    break;
            }
        }
    }

    @OnClick({R.id.verify_photo_a, R.id.verify_photo_b, R.id.verify_photo_c, R.id.verify_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.verify_photo_a:
                mCurrFileName = NAME_A;
                Intent intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                        FileUtil.getSaveFile(getApplication(), NAME_A).getAbsolutePath());
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
                startActivityForResult(intent, REQUEST_A_CAMERA);
                break;
            case R.id.verify_photo_b:
                mCurrFileName = NAME_B;
                intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                        FileUtil.getSaveFile(getApplication(), NAME_B).getAbsolutePath());
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                startActivityForResult(intent, REQUEST_B_CAMERA);
                break;
            case R.id.verify_photo_c:
                mCurrFileName = NAME_C;
                intent = new Intent(this, CameraActivity.class);
                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                        FileUtil.getSaveFile(getApplication(), NAME_C).getAbsolutePath());
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                startActivityForResult(intent, REQUEST_C_CAMERA);
                break;
            case R.id.verify_submit:
                String identNum = mVerifyIdentEdittext.getText().toString().trim();
                String realName = mVerifyRealNameEdittext.getText().toString().trim();

                if (!realName.matches("^[A-Za-z\\u4e00-\\u9fa5]+$")) {
                    ToastUtil.showShort(getString(R.string.verify_ident_name_no_right));
                    return;
                }
                if (TextUtils.isEmpty(realName)) {
                    ToastUtil.showShort(getString(R.string.verify_ident_input_name));
                    return;
                }
                if (TextUtils.isEmpty(identNum)) {
                    ToastUtil.showShort(getString(R.string.verify_ident_input_idcard));
                    return;
                }
                if (identNum.length() != 18) {
                    ToastUtil.showShort(getString(R.string.verify_ident_input_right_idcard));
                    return;
                }
                if (TextUtils.isEmpty(idCardBackImg) || TextUtils.isEmpty(idCardFrontImg) || TextUtils.isEmpty(selfieImg)) {
                    ToastUtil.showShort(getString(R.string.verify_ident_image_all));
                    return;
                }
                mPresenter.checkIdent(realName, identNum, idCardFrontImg, idCardBackImg, selfieImg);
                break;
        }
    }

    public void onSubmitVerifyResponseSuccess() {
        ToastUtil.showShort(getString(R.string.verify_ident_watting));
        SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, 2);
        setResult(RESULT_OK, new Intent().putExtra("resultData", 2));
        finish();
//        startActivity(new Intent(mActivity, CommonActivity.class));
    }

    public void updatePath(String path) {
        switch (card) {
            case 0:
                selfieImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, mVerifyPhotoA);
                break;
            case 1:
                idCardFrontImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, mVerifyPhotoB);
                break;
            case 2:
                idCardBackImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, mVerifyPhotoC);
                break;

        }

    }
}
